package com.jd.cmdb.tsc.main;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

/**
 * Created by tianzhengguo on 2016/1/21.
 */
public class WorkerNodeMain {
    public static void main(String[] args) throws Exception {
        CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
        CuratorFramework client = builder.connectString("127.0.0.1:2181")
                .sessionTimeoutMs(3000)
                .connectionTimeoutMs(3000)
                .canBeReadOnly(false)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .namespace(null)
                .defaultData(null)
                .build();
        client.start();

        String jsonData = "{\n" +
                "    \"startTime\": \"20160121094735\",\n" +
                "    \"id\": \"3\",\n" +
                "    \"subName\": \"null\",\n" +
                "    \"status\": \"2\",\n" +
                "    \"hostName\": \"bjyf-zhangjming33333\",\n" +
                "    \"endTime\": \"20160121095429\",\n" +
                "    \"taskName\": \"mdc\"\n" +
                "}";
//        client.create().forPath("/WORKERNODE/subnode1"/*,jsonData.getBytes()*/);
//        client.setData().forPath("/WORKERNODE/subnode1",jsonData.getBytes());
//        client.create().withMode(CreateMode.EPHEMERAL).forPath("/WORKERNODE/sub1",jsonData.getBytes());
        Stat stat = client.checkExists().forPath("/WORKERNODE/subnode1");
        if(stat== null)
            client.create().withMode(CreateMode.EPHEMERAL).forPath("/WORKERNODE/subnode1");
        client.setData().forPath("/WORKERNODE/subnode1",jsonData.getBytes());
        while (true){}
    }
}
